const {
	importJSON,
	showParameterMenu,
	onDblClickRow,
	onClickRow,
	deleteParameter,
	saveJSONMethod,
	openParameterDialog,
	onDblClickCell,
	onBeforeEdit,
	onAfterEdit,
} = require('./api-parameter-methods')
const {
	checkReponseParameter,
	responseOnLoadSuccess,
	showTestResponse,
} = require('./api-test-methods')
const {
	saveASTemplate,
	openTemplateListDialog,
} = require('./api-template-methods')

/**
 * 初始化响应参数控件
 * @param  {[type]} apiComp [description]
 * @return {[type]}         [description]
 */
module.exports = function initResponseParameter(apiComp) {
	let {
		resParamsComp,
		resParamsMenu,
	} = apiComp
	let flag = 'res'
	resParamsComp.treegrid({
		title: '响应参数',
		fit: true,
		fitColumns: true,
		treeField: 'name',
		lines: true,
		idField: '_treeId_',
		dblclickToEdit: false,
		clickToEdit: false,
		columns: [
			[{
				field: 'name',
				title: '变量名',
				width: 60,
				editor: {
					type: 'textbox',
				},
			}, {
				field: 'content',
				title: '含义',
				width: 50,
				editor: {
					type: 'textbox',
				},
			}, {
				field: 'type',
				title: '类型',
				width: 30,
				editor: {
					type: 'combobox',
					options: {
						valueField: 'id',
						textField: 'text',
						editable: false,
						data: [{
							id: 'string',
							text: "string"
						}, {
							id: 'number',
							text: "number"
						}, {
							id: 'boolean',
							text: "boolean",
						}, {
							id: 'array[string]',
							text: "array[string]",
						}, {
							id: 'array[number]',
							text: "array[number]",
						}, {
							id: 'array[boolean]',
							text: "array[boolean]",
						}, {
							id: 'array[object]',
							text: "array[object]",
						}, {
							id: 'object',
							text: "object",
						}],
					},
				}
			}, {
				field: 'remark',
				title: '备注',
				width: 60,
				editor: {
					type: 'textbox',
				},
				/**
				 * 检查每个返回参数
				 * 1. 参数是否存在
				 * 2. 参数类型是否正确
				 * @param  {[type]} value [description]
				 * @param  {[type]} row   [description]
				 * @return {[type]}       [description]
				 */
				formatter(value, row) {
					return checkReponseParameter.call(apiComp, value, row)
				},
			}]
		],
		toolbar: [{
			iconCls: 'icon-add',
			text: '添加参数',
			handler: function() {
				openParameterDialog.call(apiComp, flag, 'add')
			}
		}, '-', {
			iconCls: 'icon-edit',
			text: '导入JSON',
			handler: function() {
				importJSON.call(apiComp, flag)
			}
		}, '-', {
			iconCls: 'icon-back',
			text: '导入模板',
			handler: function() {
				openTemplateListDialog.call(apiComp, flag)
			}
		}, '-', {
			iconCls: 'icon-save',
			text: '保存为模板',
			handler: function() {
				saveASTemplate.call(apiComp, flag)
			}
		}, '-', {
			iconCls: 'icon-more',
			text: '查看测试返回值',
			handler: function() {
				showTestResponse.call(apiComp)
			}
		}],
		/**
		 * 右键菜单
		 * @param  {[type]} e   [description]
		 * @param  {[type]} row [description]
		 * @return {[type]}     [description]
		 */
		onContextMenu(e, row) {
			showParameterMenu.call(apiComp, e, row, flag)
		},

		/**
		 * 双击开始编辑行
		 * @param  {[type]} row [description]
		 * @return {[type]}     [description]
		 */
		onDblClickRow(row) {
			onDblClickRow.call(apiComp, row, flag)
		},

		onClickRow(row) {
			onClickRow.call(apiComp, row, flag)
		},

		onDblClickCell(field, row) {
			onDblClickCell.call(apiComp, field, row)
		},

		onLoadSuccess(row, data) {
			responseOnLoadSuccess.call(apiComp, row, data)
		},

		onBeforeEdit(row) {
			onBeforeEdit.call(apiComp, row)
		},

		onAfterEdit(row, changes) {
			onAfterEdit.call(apiComp, row, changes, flag)
		},
	});

	resParamsMenu.menu('appendItem', {
		id: 'addResponseParameterChildren',
		text: '添加属性',
		iconCls: 'icon-add',
		onclick(e) {
			openParameterDialog.call(apiComp, flag, 'child');
		},
	}).menu('appendItem', {
		id: 'delResponseParameter',
		text: '删除属性',
		iconCls: 'icon-remove',
		onclick(e) {
			deleteParameter.call(apiComp, e, flag)
		},
	}).menu('appendItem', {
		id: 'updateResponseParameter',
		text: '修改参数',
		iconCls: 'icon-edit',
		onclick(e) {
			openParameterDialog.call(apiComp, flag, 'update')
		},
	})
}